package com.data.common.util;

import com.alibaba.fastjson.JSON;
import com.data.common.aliyun.AliyunUtilFactory;
import com.data.config.AliyunConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @author xiayuanpeng
 * @since 2022/1/6
 */
@Slf4j
public class AliyunUtil {
    /**
     * alyCompanyCheck
     * @param param
     * @return
     */
    public static String alyCompanyCheck(AliyunConfig aliyunConfig, String param) {
        String host =aliyunConfig.getHost();
        String path = aliyunConfig.getPath();
        String method = aliyunConfig.getMethod();
        String appcode = aliyunConfig.getAppCode();
        // 返回结果定义
        String returnStr = null;
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        try {
            url = new URL(host+path + "?com=" + param);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("Authorization", "APPCODE " + appcode);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod(method);
            // 不用缓存
            httpURLConnection.setUseCaches(false);
            httpURLConnection.connect();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            returnStr = buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return returnStr;
    }

    public static final String ALIYUN__IDCARD_VERTIFY_SUCCESS_CODE = "0";

    /**
     * 二要素校验
     * @param name
     * @param idcard
     * @return
     */
    public static boolean verifyIdcard(String name, String idcard) {
        try {
            log.info("=verifyIdcard==name:{},idcard:{}",name,idcard);
            Map<String, Object> stringObjectMap = AliyunUtilFactory.build().alyTwoElements(name, idcard);
            log.info("==stringObjectMap:{}", JSON.toJSONString(stringObjectMap));
            if (ALIYUN__IDCARD_VERTIFY_SUCCESS_CODE.equals(stringObjectMap.get("code"))){
                return true;
            }
        } catch (Exception e) {
            log.error("aliyun vertity idcar error",e.getMessage(),e);
        }
        return false;

    }

}
